package com.wallace.jsonrequester;

import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

public class JsonRequest {

    public JsonRequest() {
    }

    /**
     * @author torgis
     * @param args
     * @throws JSONException 
     */
    public static void main(String[] args) throws JSONException {
    	
    	Utils utils = new Utils();
        
        String json = "{GroupBy : [City],Filters : [{FieldName : ArrestDateTime,Value :Date(1197299944000),Operator : GreaterThanOrEqual},{FieldName : ArrestDateTime,Value : Date(1199978344000),Operator : LessThanOrEqual}],Aggregates : [{Function : Count}]}";
        
        JSONTokener jt = new JSONTokener(json);
        JSONObject jo2 = new JSONObject(jt);
        System.out.println("Created JSON Object out of input string, result is: " + jo2.toString());
        
        //Build the JSON object
        JSONObject j = new JSONObject();
        j.put("UserName", utils.getUsername());
        j.put("Password", utils.getPassword());
        System.out.println(j.toString());
        
        //initialize the cookie manager for the web requests
        CookieStore cookieStore = new BasicCookieStore();
        
        BasicHttpContext localContext = new BasicHttpContext();
        localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
        
        //This is the request for authentication, response is stored in a cookie in localContext
        //which is passed in httpClient.execute()
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = null;
        try {
            HttpPost request = new HttpPost(utils.getServerAuthString());
            StringEntity params = new StringEntity(j.toString());
            request.addHeader("content-type", "application/json;charset=utf-8");
            request.setEntity(params);
            response = httpClient.execute(request, localContext);
            System.out.println("Status of authentication: " + response.getStatusLine());
        }catch (Exception ex) {
            System.out.println("Failed with exception " + ex.toString());
        } finally {
            httpClient.getConnectionManager().shutdown();
        }
        
        //Using the cookie stored in localContext, make a JSON request to the server for records
        HttpClient httpClient2 = new DefaultHttpClient();
        try {
            HttpPost request = new HttpPost(utils.getServerRequestString());
            StringEntity params = new StringEntity(jo2.toString());
            request.addHeader("content-type", "application/json;charset=utf-8");
            request.setEntity(params);
            response = httpClient2.execute(request, localContext);
            System.out.println("Status of authentication: " + response.getStatusLine());
        }catch (Exception ex) {
            System.out.println("Failed with exception " + ex.toString());
        } finally {
            httpClient2.getConnectionManager().shutdown();
        }
        
    }
}
